//
//  HEShare.m
//  huien
//
//  Created by yiban on 2017/7/6.
//  Copyright © 2017年 lcb. All rights reserved.
//

#import "HEShare.h"
#import <LocalAuthentication/LocalAuthentication.h>
#import "MBProgressHUD+JDragon.h"
//APP ID1105179820APP KEYmC4WQvx99qAAKTAe
@implementation HEShare
LcbSingletonM(HEShare)
@synthesize authStatus=_authStatus;
@synthesize selectedIndex=_selectedIndex;

+(SessionModel*)sessionModel
{
    id model=[[LcbCache sharedCache] objectForKey:@"SessionModel"];
    if (model && [NSStringFromClass([model class]) isEqualToString:@"SessionModel"])
    {
        return model;
    }
    return nil;
}
+(BOOL)isLogin
{
    SessionModel *session = [self sessionModel];
    if (session==nil || session.sessionId.length==0) {
        return NO;
    }
     return YES;
}

+(void)logout:(void(^)(id))completion
{
    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"patternStatus"];
    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"fingerprintStatus"];
    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"patternLock"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [[LcbCache sharedCache] removeAllObjects];
    [[LcbCache sharedCache] removeObjectForKey:@"SessionModel" withBlock:^(NSString * _Nonnull key) {
        if (completion) {
            completion(key);
        }
    }];
}
+(NSString*)sessionId
{
    id model=[[LcbCache sharedCache] objectForKey:@"sessionId"];
    if (model)
    {
        return model;
    }
    return nil;
}
+(BOOL)isGestureOpen
{
    NSString *patternStatus =[[NSUserDefaults standardUserDefaults] objectForKey:@"patternStatus"];
    NSString *fingerprintStatus =[[NSUserDefaults standardUserDefaults] objectForKey:@"fingerprintStatus"];
    if ((patternStatus && [patternStatus integerValue]==1)||(fingerprintStatus && [fingerprintStatus integerValue]==1)) {
        return YES;
    }
     return NO;
}
+(BOOL)isPatternOpen
{
    NSString *patternStatus =[[NSUserDefaults standardUserDefaults] objectForKey:@"patternStatus"];
    if ((patternStatus && [patternStatus integerValue]==1)) {
        return YES;
    }
    return NO;
}
+(BOOL)isFingerOpen
{
    NSString *fingerprintStatus =[[NSUserDefaults standardUserDefaults] objectForKey:@"fingerprintStatus"];
    if ((fingerprintStatus && [fingerprintStatus integerValue]==1)) {
        return YES;
    }
    return NO;
}
+(void)fingerCert
{
    NSString *fingerprintStatus =[[NSUserDefaults standardUserDefaults] objectForKey:@"fingerprintStatus"];
    if(fingerprintStatus && [fingerprintStatus integerValue]==1 )
    {
        LAContext *laContext = [[LAContext alloc]init];
        NSError *err = nil;
        __weak typeof(self) weakSelf = self;
        //判断设备是否支持指纹识别
        BOOL isSupport = [laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&err];
        if (isSupport) {
            [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"通过验证指纹解锁" reply:^(BOOL success, NSError *error) {
                if (success) {
                    [weakSelf loginwithgesture];
                    return ;
                }
                else
                {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [YBToast showWithText:@"验证指纹解锁失败！"];
                    });
                }
            }];
        }
    }
}
+(void)loginwithgesture
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD showTipMessageInWindow:@"登录中..." timer:1000];
    });
    
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:[[LcbCache sharedCache] objectForKey:@"userName"] forKey:@"userName"];
    [dict setValue:[[LcbCache sharedCache] objectForKey:@"password"] forKey:@"password"];
    [HEApi loginWithParam:dict serviceblock:^(id response, NSString *errorMsg) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
        });
        if (response) {
            if ([response isKindOfClass:[SessionModel class]]) {
                SessionModel *sessionModel = (SessionModel*)response;
                [[JGShare sharedJGShare] setPushAlias:[NSString stringWithFormat:@"%ld",(long)sessionModel.session_user.userId]];
                [[LcbCache sharedCache] setObject:sessionModel forKey:@"SessionModel"];
            
                
            }
        }
        
    }];
}
@end
